package com.atguigu.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.atguigu.product.entity.BrandEntity;
import com.atguigu.product.entity.CategoryBrandRelationEntity;
import com.atguigu.product.vo.BrandVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.product.service.CategoryBrandRelationService;
import com.atguigu.common.utils.R;


/**
 * 品牌分类关联
 *
 * @author mrs
 * @email 2314028487@qq.com
 * @date 2022-10-11 21:08:34
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;


    // /product/categorybrandrelation/brands/list

    /**
     * 获取当前分类关联的所有品牌
     *
     * @param catId
     * @return
     */
    @GetMapping("/brands/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R brandList(@RequestParam Long catId) {

        List<BrandEntity> brands = categoryBrandRelationService.getBrandsById(catId);
        List<BrandVo> vos = brands.stream().map((brand) -> {
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(brand.getBrandId());
            brandVo.setBrandName(brand.getName());
            return brandVo;
        }).collect(Collectors.toList());

        return R.ok().put("data", vos);
    }


    /**
     * 获取品牌相关分类
     */
    @GetMapping("/catelog/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Long brandId) {

        LambdaQueryWrapper<CategoryBrandRelationEntity> queryWrapper =
                new LambdaQueryWrapper<CategoryBrandRelationEntity>();

        queryWrapper.eq(CategoryBrandRelationEntity::getBrandId, brandId);

        List<CategoryBrandRelationEntity> list = categoryBrandRelationService.list(queryWrapper);

        return R.ok().put("data", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
